package org.enate.bloatterrorist;

import org.enate.bloatterrorist.AppListFragment.PInfo;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public final class EventsFragment extends Fragment {
    private static final String TAG = "EventsFragment";
    private static View v;
    protected static ListView lv;
    protected static TextView tv_banner, tv_noevents;
    private static ImageView iv;
    private static Drawable dw;
    private static Button btn_newevent, btn_back;
    protected static PInfo mPInfo;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_events, container, false);
        return v;
    }

    @Override public void onActivityCreated(Bundle savedInstanceState) {
    	super.onActivityCreated(savedInstanceState);
    	v = getView();
    	lv = (ListView) v.findViewById(R.id.lv_eventlist);
    	tv_banner = (TextView) v.findViewById(R.id.tv_banner);
    	tv_noevents = (TextView) v.findViewById(R.id.tv_no_eventlist);
    	btn_back = (Button) v.findViewById(R.id.btn_back);
    	btn_back.setOnClickListener(new OnClickListener(){@Override public void onClick(View arg0) {loadEventsSaved();}});
    	btn_newevent = (Button) v.findViewById(R.id.btn_newevent);
    }  

    private static void buildEvent(PInfo pinfo){
    	if(null!=pinfo){Main.makeToast("TODO: build event for "+pinfo.appname);}
    	else{Main.makeToast("TODO: build global event");}
    }

    protected static void loadEventsSaved(){
    	String[] savedevents = Main.prefs.getString("savedevents", "").split("\\|");
    	if(savedevents.length>0 && !savedevents[0].equals("")){
    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.c,android.R.layout.simple_list_item_1,savedevents);
    		tv_noevents.setVisibility(View.GONE);
    		lv.setVisibility(View.VISIBLE);
    		lv.setAdapter(adapter);
    		adapter.notifyDataSetChanged();
    	}
    	else {
    		lv.setVisibility(View.GONE);
    		tv_noevents.setVisibility(View.VISIBLE);
    		tv_noevents.setText(Main.c.getString(R.string.fragment_events_tv_no_eventlist));
    	}
    	btn_back.setVisibility(View.GONE);
    	final Rect rdwicon = Main.c.getResources().getDrawable(R.drawable.ic_launcher).getBounds();
    	Drawable dwicon = Main.c.getResources().getDrawable(R.drawable.ic_launcher);
    	dwicon.mutate().setBounds(0, 0, 48, 48);
    	tv_banner.setCompoundDrawablesWithIntrinsicBounds(dwicon, null, null, null);
    	Main.c.getResources().getDrawable(R.drawable.ic_launcher).setBounds(rdwicon);
    	tv_banner.setText(Main.c.getString(R.string.fragment_events_tv_banner));
    	btn_newevent.setOnClickListener(new OnClickListener(){@Override public void onClick(View arg0) {buildEvent(null);}});
    }
    

    protected static void loadEventsForPInfo(final PInfo pinfo){
    	String[] eventsForPInfo = Main.prefs.getString(pinfo.pname+"_events", "").split("\\|");	
    	pinfo.icon.setBounds(0, 0, 48, 48);
    	tv_banner.setCompoundDrawables(pinfo.icon, null, null, null);
    	tv_banner.setText(pinfo.appname);
    	
    	if(eventsForPInfo.length>0 && !eventsForPInfo[0].equals("")){
    		Main.makeToast("Found "+eventsForPInfo.length+" "+(1==eventsForPInfo.length?"event":"events")+" for "+pinfo.appname);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.c,android.R.layout.simple_list_item_1,eventsForPInfo);
            tv_noevents.setVisibility(View.GONE);
            lv.setVisibility(View.VISIBLE);
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();  
    	}
    	else{
    		Main.makeToast("Found no events for "+pinfo.appname);
    		lv.setVisibility(View.GONE);
    		tv_noevents.setVisibility(View.VISIBLE);
    		tv_noevents.setText(Main.c.getString(R.string.fragment_events_tv_no_eventlist_withappcontext));
    	}
    	btn_back.setVisibility(View.VISIBLE);
    	btn_newevent.setOnClickListener(new OnClickListener(){@Override public void onClick(View arg0) {buildEvent(pinfo);}});
    }    
    
    protected static void loadActivities(PInfo pinfo) {
    	String mActivity = "";
    	API.arr_ai = API.getActivitiesByPname(pinfo.pname);
    	if(null!=API.arr_ai) {
        	String[] items_list=new String[API.arr_ai.length];
        	for(int a=0; a<API.arr_ai.length; a++){
        		mActivity = API.arr_ai[a].name.replace(API.arr_ai[a].packageName+".", "");
        		
        		Log.d(TAG,
        				 "[."+mActivity+"]; "
        				+"["+API.getLauncherActivityName(pinfo.pname)+"]; "
        				+"["+mActivity.replace(API.arr_ai[a].packageName, API.arr_ai[a].packageName+"/")+"] ----------------------"
        		);
        		
        		if(!mActivity.equals("")){
        			if(("."+mActivity).equals(API.getLauncherActivityName(pinfo.pname))){
        				mActivity+=" (main class)";
        			}
        		}
        		items_list[a] = mActivity;//.replace(API.arr_ai[a].packageName+".", "");	
        	}
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.c,android.R.layout.simple_list_item_1,items_list);
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
    	}
    }
    
}
